<!--
 * @Description: 告警联系人管理页面
-->
<template>
  <BaseMainSlot>
    <template v-slot:btn-box>
      <ActionBar
        :add-active="addActive"
        :del-active="delActive"
        :edit-active="editActive"
        :cancel-active="cancelActive"
        :save-active="saveActive"
        @add="add"
        @cancel="cancel"
        @edit="edit"
        @del="del"
        @save="save"
      />
    </template>
    <template v-slot:search-box>
      <SearchBox @search="search">
        <div class="common-search-item">
          <div class="label">联系人:</div>
          <div class="input">
            <el-input v-model="searchList.alarmContactName" clearable />
          </div>
        </div>
      </SearchBox>
    </template>
    <template v-slot:table-box>
      <el-table
        v-loading="loading"
        :data="tableData"
        stripe
        border
        height="88%"
        @selection-change="handleSelectionChange"
        @sort-change="changeTableSort"
      >
        <el-table-column type="selection" width="55" :selectable="row => { return !row.indflag }" />
        <el-table-column type="index" label="序号" width="100" />
        <el-table-column prop="alarmContactName" label="姓名" sortable="custom" />
        <el-table-column prop="phone" label="手机号22" sortable="custom" />
        <el-table-column prop="weChat" label="微信" sortable="custom" />
        <el-table-column prop="email" label="邮箱" sortable="custom" />
        <el-table-column width="120" label="操作">
          <template v-if="scope.row.alarmContactId" slot-scope="scope">
            <el-button
              class="text-btn-cursor"
              type="text"
              size="small"
              @click="handleEdit(scope.row)"
            ><i class="el-icon-edit-outline"></i> 修改</el-button>
            <el-button
              class="text-btn-cursor cursor-delete"
              type="text"
              size="small"
              @click="handleDelete(scope.row)"
            ><i class="el-icon-delete"></i> 删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :page-size="pageSize"
        :current-page="currentPage"
        :total-size="totalSize"
        @changePageSize="changePageSize"
        @changeCurrentPage="changeCurrentPage"
      />
    </template>
    <template v-slot:dialog>
      <el-dialog
        :title="dialogTitle + '告警联系人信息'"
        :visible.sync="dialogFormVisible"
        :before-close="cancel"
        width="40%"
      >
        <el-form
          ref="formData"
          label-position="left"
          :model="formData"
          :rules="rules"
          :show-message="canInput"
          class="el-form"
        >
          <el-form-item label="姓名:" class="w50p" prop="alarmContactName">
            <el-input v-model="formData.alarmContactName" clearable />
          </el-form-item>
          <el-form-item label="手机号:" class="w50p" prop="phone">
            <el-input v-model="formData.phone" clearable />
          </el-form-item>
          <el-form-item label="微信:" class="w50p">
            <el-input v-model="formData.weChat" clearable />
          </el-form-item>
          <el-form-item label="邮箱:" class="w50p">
            <el-input v-model="formData.email" clearable />
          </el-form-item>
        </el-form>
        <div v-if="nowChanceStau !== ''" slot="footer" class="dialog-footer">
          <el-button class="zh-button-one" @click="cancel('formData')">
            取 消
          </el-button>
          <el-button
            class="zh-button-two"
            type="primary"
            @click="save('formData')"
          >
            保 存
          </el-button>
        </div>
      </el-dialog>
    </template>
  </BaseMainSlot>
</template>

<script>
import ActionBar from "@/components/ActionBar";
import Pagination from "@/components/Pagination";
import nowpagereq from "@/api/digital-production/monitoring-alarm/alarm-contact-management";
import BaseMainSlot from "@/components/BaseMainSlot/newBaseMainSlot";
import { mymixin } from "@/utils/mixin";
import SearchBox from "@/components/SearchBox";
export default {
  name: "AlarmContactManage",
  components: { ActionBar, Pagination, BaseMainSlot, SearchBox },
  mixins: [mymixin],
  data() {
    return {
      rules: {
        alarmContactName: [
          { required: true, message: "请填写姓名", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "请填写手机号", trigger: "blur" },
          { type: "number", message: "手机号为数字" }
        ]
      },
      // 搜索内容
      searchList: {},
      // 选中的数据项数组 alarmContactId组成 (此数组最多为一个)
      selectIdList: [],
      dialogTitle: "",
      dialogFormVisible: false
    };
  },
  watch: {},
  created() {
    // 获取表格数据
    this.getTableDataList();
  },
  mounted() {},
  methods: {
    getTableDataList() {
      this.loading = true;
      let params = {
        page: this.currentPage,
        size: this.pageSize,
        sortList: this.sortList || [],
      };
      if (
        this.searchList.alarmContactName &&
        this.searchList.alarmContactName.length > 0
      ) {
        let searchparams = {
          alarmContactName: this.searchList.alarmContactName
        };
        nowpagereq.querybycondition(searchparams).then(res => {
          this.loading = false;
          if (res.code === 1) {
            // this.$message.success('查询成功')
          } else {
            this.$message.error("查询失败");
          }
          let data = res.data;
          // 赋值总条数
          this.totalSize = data.length;
          // 赋值数组
          this.tableData = data;
          this.addblankmet();
          this.resetActionBar();
        });
      } else {
        nowpagereq.getlist(params).then(res => {
          this.loading = false;
          if (res.code === 0) {
            this.$message.error("查询失败");
          }
          let data = res.data;
          // 赋值总条数
          this.totalSize = data.totalElements;
          // 赋值数组
          this.tableData = data.content;
          this.addblankmet();
          this.resetActionBar();
        });
      }
    },
    search() {
      this.currentPage = 1;
      this.getTableDataList();
    },
    changePageSize(pageSize) {
      this.pageSize = pageSize;
      this.getTableDataList();
    },
    changeCurrentPage(currentPage) {
      this.currentPage = currentPage;
      this.getTableDataList();
    },
    // 添加
    add() {
      this.openDialog("add");
      this.formData = {};
    },
    // 取消
    cancel() {
      this.closeDialog();
      this.formData = {};
    },
    handleEdit(row) {
      if (row.alarmContactId) {
        this.formData = row;
        this.openDialog("edit");
      }
    },
    handleSelectionChange(list) {
      this.selectIdList = [];
      let notvalue = true;
      this.selectIdList = list.map(item => {
        if (item.alarmContactId) {
          return item.alarmContactId;
        } else {
          notvalue = false;
          return "";
        }
      });
      if (notvalue === false) {
        this.$message.warning("不可选择空数据！");
        return;
      }
      if (this.selectIdList.length > 0) this.delActive = true;
      else this.delActive = false;
    },
    delmessage() {
      nowpagereq.dellist(this.selectIdList).then(res => {
        if (res.code === 1) {
          this.$message.success("删除成功");
          this.getTableDataList();
        } else {
          this.$message.error("删除失败");
        }
      });
    },
    handleDelete(data) {
      // nowpagereq.dellist([data.alarmContactId]).then(res => {
      //   if (res.code === 1) {
      //     this.$message.success("删除成功");
      //     this.getTableDataList();
      //   } else {
      //     this.$message.error("删除失败");
      //   }
      // });
      this.$confirm("此操作将永久删除该条数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        customClass: "myClass"
      })
        .then(() => {
          nowpagereq.dellist([data.alarmContactId]).then(res => {
            if (res.code === 1) {
              this.$message.success("删除成功");
              this.getTableDataList();
            } else {
              this.$message.error("删除失败");
            }
          });
        })
        .catch(() => {
          //
          this.$message("取消删除");
        });
    },
    save() {
      if (this.nowChanceStau === "edit") {
        // 编辑接口
        // this.formData.routePointMapperList = this.tagsList;
        nowpagereq.editlist(this.formData).then(res => {
          if (res.code === 1) {
            this.$message.success("修改成功！");
            this.closeDialog();
            this.getTableDataList();
          } else {
            this.$message.error("修改失败！");
          }
        });
      } else if (this.nowChanceStau === "add") {
        nowpagereq.addlist(this.formData).then(res => {
          if (res.code === 1) {
            this.$message.success("新增成功！");
            this.closeDialog();
            this.getTableDataList();
          } else {
            this.$message.error("新增失败！");
          }
        });
      }
    },
    openDialog(type) {
      this.dialogFormVisible = true;
      // eslint-disable-next-line eqeqeq
      this.dialogTitle = type == "add" ? "新增" : "编辑";
      this.nowChanceStau = type;
    },
    closeDialog() {
      this.dialogFormVisible = false;
      this.dialogTitle = null;
      this.nowChanceStau = "";
    }
  }
};
</script>

<style scoped lang="scss"></style>
